package com.kin207.netty.message.mq;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

import com.kin207.netty.server.IServer;
import com.kin207.netty.server.ServerManager;
import com.kin207.netty.session.SessionManager;

@Component
public class MqMessageListener implements MessageListener{
	final static Logger logger = LoggerFactory.getLogger(MqMessageListener.class);
	
	@Autowired
	private JmsTemplate jmsTemplate;
	
	public void onMessage(Message msg) {
		TextMessage txtmsg = (TextMessage)msg;
		String msgId = null; 
		Destination replyTo = null;
		String jmsCorrelationID = null;
		try {
			msgId = txtmsg.getJMSMessageID();
			replyTo = txtmsg.getJMSReplyTo();
			String message = txtmsg.getText();
			String groupId = txtmsg.getStringProperty("JMSXGroupID");
			String cid = txtmsg.getStringProperty("cid");
			String sid = txtmsg.getStringProperty("sid");
			jmsCorrelationID = txtmsg.getJMSCorrelationID();
			String msgType =  txtmsg.getStringProperty("messageType");
			
			logger.debug("on message, JMSXGroupID:{}, JMSReplyTo:{}, jmsCorrelationID:{}, messageId:{}, messageType:{}, cid:{}, sid:{}, message:{}"
					, groupId, replyTo, jmsCorrelationID, msgId, msgType,
					cid, sid, msg);
			
			int code = -1;
			
			IServer server = ServerManager.getShared().getServer(sid);
			SessionManager sm = server.getSessionManager();
			if(msgType==null || "s2p".equals(msgType)){
				code = sm.sendMessage(cid, message,System.currentTimeMillis());
			}else if("s2g".equals(msgType)){
				String groupName =  txtmsg.getStringProperty("groupName");
				code = sm.sendGroupMessage(groupName, message,System.currentTimeMillis());
			}else if("s2s".equals(msgType)){
				code = sm.sendSystemMessage(message,System.currentTimeMillis());
			}
			replyTo(replyTo ,jmsCorrelationID, msgId, String.valueOf(code), null);
		} catch (JMSException e) {
			logger.error(e.getMessage(), e);
			replyTo(replyTo, jmsCorrelationID, msgId, "-1", e.getMessage());
		}
	}
	
	private void replyTo(Destination replyTo, final String jmsCorrelationID, final String msgId, String code, String errInfo){
		if(replyTo==null){
			return;
		}
		final String replyMessage = "{\"code\":\""+code+"\", \"message\":\""+(errInfo)+"\"}";
		logger.debug("reply to, queueName:{}, jmsCorrelationID:{}, messageId:{}, code:{}, errInfo:{}, message:{}", replyTo, jmsCorrelationID, msgId, code, errInfo, replyMessage);
		jmsTemplate.send(replyTo, new MessageCreator() {
			public Message createMessage(javax.jms.Session session) throws JMSException {
				TextMessage msg = session.createTextMessage(replyMessage);
				msg.setJMSMessageID(msgId);
				msg.setJMSCorrelationID(jmsCorrelationID);
				return msg;
			}
		});
		logger.debug("reply to send success.");
	}
}
